package com.heima.wemedia.controller.v1;

import com.heima.api.wemedia.WmNewsControllerApi;
import com.heima.model.common.constants.WemediaContants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {
    @Autowired
    private WmNewsService wmNewsService;
    /**
     * 分页带条件查询自媒体文章列表
     * @param wmNewsPageReqDto
     * @return
     */
    @PostMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    /**
     * 提交文章
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        if(WmNews.Status.SUBMIT.getCode()==dto.getStatus()){
            return wmNewsService.saveNews(dto, WemediaContants.WM_NEWS_SUMMIT_STATUS);
        }
        return wmNewsService.saveNews(dto, WmNews.Status.NORMAL.getCode());
    }

    /**
     * 根据id获取文章信息
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    /**
     * 上下架
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    @DeleteMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }


    /**
     * 根据id查询文章
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    /**
     * 修改文章
     *
     * @param wmNews
     * @return
     */
    @Override
    @PutMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        if(wmNewsService.updateById(wmNews)){
            return new ResponseResult().ok(true);
        }
        return new ResponseResult().ok(false);
    }
}
